﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Threading;
using System.Globalization;
using System.Configuration;
using System.Data;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace ProjectSem40701
{
    public partial class Register : System.Web.UI.Page
    {
        /// <summary>
        /// Declare member variables here 
        /// </summary>
        private const string SUCCESS = "success";
        private const string FAIL = "fail";
        private const string NOT_CHECKED = "notChecked";
        private const string UNKNOW_ERROR = "ukErr";
        private const string DUPLICATE_USER_NAME = "dcUser";
        private const string DUPLICATE_EMAIL = "dcEmail";
        private const string INVALID_EMAIL = "invalidEm";
        private const string INVALID_PASSWORD = "invalidPw";
        private const string INVALID_ANSWER = "invalidAsw";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //Add user here
        protected void btnAddUser_Click(object sender, EventArgs e)
        {
            try
            {
                
                MembershipCreateStatus createStatus;
                MembershipUser newUser = Membership.CreateUser(txtUserName.Text, txtPassword.Text, txtMail.Text, txtQuestion.Text, txtAnswer.Text, true, out createStatus);
                
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
                DateTime mydt = Convert.ToDateTime(this.txtBDay.Text);
                mydt = Convert.ToDateTime(mydt);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    MainDataset.tbl_UserTableAdapters.tbl_UserTableAdapter dpUser =
                        new MainDataset.tbl_UserTableAdapters.tbl_UserTableAdapter();
                    Guid UserID = (Guid)newUser.ProviderUserKey;
                    int ok = dpUser.tbl_User_Insert(UserID, this.txtIDCard.Text.Trim(), this.txtFName.Text.Trim(), this.txtLName.Text.Trim(),
                        String.Empty, this.txtPhone.Text.Trim(), String.Empty, (this.rdbtnMale.Checked ? true : false), mydt, String.Empty, 1);
                    
                    if (ok > 0)
                    {
                        //thanh cong
                        this.RequiredFieldValidator2.Enabled = false;
                        this.RequiredFieldValidator3.Enabled = false;
                        Show_Dialog(SUCCESS);
                    }
                    else
                    {
                        //that bai
                        Show_Dialog(UNKNOW_ERROR);
                    }
                }
                else
                {
                    switch (createStatus)
                    { 

                        case MembershipCreateStatus.DuplicateUserName:
                                Show_Dialog(DUPLICATE_USER_NAME);
                            break;
                        case MembershipCreateStatus.DuplicateEmail:
                                Show_Dialog(DUPLICATE_EMAIL);
                            break;
                        case MembershipCreateStatus.InvalidEmail:
                                Show_Dialog(INVALID_EMAIL);
                            break;
                        case MembershipCreateStatus.InvalidPassword:
                                Show_Dialog(INVALID_PASSWORD);
                            break;
                        case MembershipCreateStatus.InvalidAnswer:
                                Show_Dialog(INVALID_ANSWER);
                            break;
                        default:
                                Show_Dialog(UNKNOW_ERROR);
                            break;
                    }
                }
            }
            catch
            {
                //bao loi            
                Show_Dialog(UNKNOW_ERROR);
            }
        }


        /// <summary>
        /// Thông báo thành công hay thất bại ở đây
        /// </summary>
        /// <param name="Check"></param>
        private void Show_Dialog(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                        this.ltrMessage.Text = "Đăng ký thành công";
                    break;
                case FAIL:
                        this.ltrMessage.Text = "Đăng ký thất bại";
                    break;
                case DUPLICATE_USER_NAME:
                        this.ltrMessage.Text = "Tài khoản đã có người sử dụng";
                    break;

                case DUPLICATE_EMAIL:
                        this.ltrMessage.Text = "Email đã có người sử dụng";
                    break;

                case INVALID_EMAIL:
                        this.ltrMessage.Text = "Email không đúng định dạng";
                    break;
                case INVALID_PASSWORD:
                        this.ltrMessage.Text = "Password không hợp lệ";
                    break;
                case INVALID_ANSWER:
                        this.ltrMessage.Text = "Câu trả lời bí mật không hợp lệ";
                    break;
                default:
                        this.ltrMessage.Text = "Lỗi không xác định, đăng ký thất bại";
                    break;
            }
            if (Check == SUCCESS)
            {
                this.lblSuccess.Text = Check;
            }
            else 
            {
                this.lblSuccess.Text = FAIL;
                this.RequiredFieldValidator2.Enabled = false;
                this.RequiredFieldValidator3.Enabled = false;
            }

            this.btnAdd_ModalPopupExtender.Enabled = true;
            this.btnAdd_ModalPopupExtender.Show();
        }
        /// <summary>
        /// Xay ra khi nguoi dung an nut Close
        /// </summary>
        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        /// <summary>
        /// Xay ra khi nguoi dung an nut Yes
        /// </summary>
        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        /// <summary>
        /// Close dialog
        /// </summary>
        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            {
                case SUCCESS:
                        Response.Redirect("Default.aspx");
                    break;
                case FAIL:
                        this.btnAdd_ModalPopupExtender.Enabled = false;
                        this.RequiredFieldValidator2.Enabled = true;
                        this.RequiredFieldValidator3.Enabled = true;
                    break;
                default:
                        this.btnAdd_ModalPopupExtender.Enabled = false;
                        this.RequiredFieldValidator2.Enabled = true;
                        this.RequiredFieldValidator3.Enabled = true;
                    break;
            }
        }

    }
}
